package com.firebird.app.kotlin.stock.trend.activity

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.widget.AppCompatImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.firebird.app.kotlin.stock.trend.fragment.FragmentNavigatorA
import com.firebird.app.kotlin.stock.trend.fragment.FragmentNavigatorB
import com.firebird.app.kotlin.stock.trend.service.StockService
import com.firebird.app.kotlin.stock.trend.view.FooterMenuView
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.adapter.NavigatorFragmentAdapter
import java.util.*


/**
 * app首页界面
 * @author ljk
 * @date   2019年10月5日
 */
class HomeActivity : FragmentActivity() {
    private var isExit = false
    private var viewPager: ViewPager? = null
    private var menu: FooterMenuView? = null
    private var dots: Array<AppCompatImageView?>?=null
    //顶部导航处当前小圆点的坐标
    private var navCurrentIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_home)
        viewPager = findViewById<View>(R.id.vp_fragment) as ViewPager
        val fragmentList: ArrayList<Fragment> = ArrayList<Fragment>()
        val fragmenta = FragmentNavigatorA(this)
        val fragmentb = FragmentNavigatorB(this)
        fragmentList.add(fragmenta)
        fragmentList.add(fragmentb)
        viewPager!!.adapter = NavigatorFragmentAdapter(supportFragmentManager, fragmentList)
        viewPager!!.addOnPageChangeListener(NavigatorPageChangeListener())
        initDot()
        //初始化底部菜单控件
        menu = findViewById<View>(R.id.footer_menu) as FooterMenuView
        menu!!.setCurrentMenu(R.id.menu_home)
        menu!!.setOnTouchListener(menu!!.MenuOnTouchListener())
    }

    override fun onRestart() {
        super.onRestart()
        menu!!.setCurrentMenu(R.id.menu_home)
    }

    internal inner class NavigatorPageChangeListener : OnPageChangeListener {
        override fun onPageScrollStateChanged(arg0: Int) {}
        override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {}
        override fun onPageSelected(position: Int) {
            setCurrentDot(position)
        }
    }

    //初始化底部小圆点
    private fun initDot() {
        val ll = findViewById<View>(R.id.lay_dot) as LinearLayout
        val size = viewPager!!.adapter!!.count
        dots = arrayOfNulls(size)
        for (i in dots!!.indices) {
            dots!![i] = ll.getChildAt(i) as AppCompatImageView
            dots!![i]!!.tag = i
            //dots[i].setEnabled(true);
//给下方小圆点绑定点击事件
            dots!![i]!!.setOnClickListener { v ->
                val position = v.tag as Int //获得点击的小圆点位置
                setCurrentDot(position)
                setCurrentView(position)
            }
        }
        dots!![navCurrentIndex]!!.isEnabled = false //初始化时默认选中第一个小圆点
    }

    //设置小圆点被选中状态
    private fun setCurrentDot(position: Int) {
        if (position < 0 || position >= viewPager!!.adapter!!.count || position == navCurrentIndex) {
            return
        }
        dots!![position]!!.isEnabled = false
        dots!![navCurrentIndex]!!.isEnabled = true
        navCurrentIndex = position
    }

    //点击小圆点后切换到响应的页面
    private fun setCurrentView(position: Int) {
        if (position < 0 || position > viewPager!!.adapter!!.count) {
            return
        }
        viewPager!!.currentItem = position
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click()
        }
        return false
    }

    /**
     * 双击退出程序
     */
    private fun exitBy2Click() {
        if (isExit == false) {
            isExit = true
            Toast.makeText(this, "再点一次退出", Toast.LENGTH_SHORT).show()
            Timer().schedule(object : TimerTask() {
                override fun run() {}
            }, 2000) // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else { //关闭后台服务
            stopService(Intent(this, StockService::class.java))
            finish()
            System.exit(0)
        }
    }
}